#  Replication code to make data for
#  "The Limited Value of a Second Opinions"
#  William Minozzi and Jonathan Woon
#  2019/06/06

# Set up environment ----
library(data.table)
library(readstata13)
library(plyr)
library(weights)

# Define functions ----
arctanh <- function(x) {
  1/2 * log((1 + x)/(1 - x))
}

# Make subject data ----
Subject_Data <- readstata13::read.dta13("data/compcom-subjects.dta")
Subject_Data$Session <- plyr::mapvalues(Subject_Data$session,
  from = c(
    3, 4, 9, 10, 13, 18,
    1, 6, 8, 12, 15, 16,
    2, 5, 7, 11, 14, 17),
  to = 1:18)


# Make all data ----
All_Data <- readstata13::read.dta13("data/compcom-game.dta")
All_Data$Session <- plyr::mapvalues(All_Data$session,
  from = c(
    3, 4, 9, 10, 13, 18,
    1, 6, 8, 12, 15, 16,
    2, 5, 7, 11, 14, 17),
  to = 1:18)
All_Data <- subset(All_Data, type != 9)   # drop dummy subjects
All_Data[All_Data == "-999"] <- NA        # recode missing values as NAs
All_Data$action[All_Data$type != 0] <- NA # set actions to missing for senders
All_Data <- plyr::rename(All_Data, c("period" = "Period", "action" = "Action",
  "target" = "Target", "group" = "Group", "type" = "Type", "id_a" = "ID_a",
  "id_b" = "ID_b", "id_c" = "ID_c"))
All_Data <- plyr::arrange(All_Data, Session, Period, Group, Type)
All_Data$Treatment <- plyr::mapvalues(All_Data$treat,
  from = c("SINGLE", "SAME", "OPP"), to = c("Single", "Aligned", "Opposed"))
All_Data$Treatment <- factor(All_Data$Treatment,
  levels = c("Single", "Aligned", "Opposed"))
All_Data$Type <- plyr::mapvalues(All_Data$Type, from = c(0, 1, 2),
  to = c("Receiver", "Moderate Sender", "Extremist Sender"))
All_Data$Type <- factor(All_Data$Type,
  levels = c("Extremist Sender", "Moderate Sender", "Receiver"))
# generate session-identifying ID numbers
All_Data <- plyr::mutate(All_Data, ID = 100 * Session + subject)
All_Data <- plyr::mutate(All_Data, ID_a = 100 * Session + ID_a)
All_Data <- plyr::mutate(All_Data, ID_b = 100 * Session + ID_b)
All_Data <- plyr::mutate(All_Data, ID_c = 100 * Session + ID_c)
# generate new variables
All_Data$Receiver_ID <- NA
ok <- with(All_Data, Type == "Receiver")
All_Data$Receiver_ID[ok] <- All_Data$ID[ok]
All_Data$Moderate_ID <- NA
ok <- with(All_Data, Type == "Moderate Sender" & Treatment == "Single")
All_Data$Moderate_ID[ok] <- All_Data$ID[ok]
ok <- with(All_Data, Type == "Moderate Sender" & Treatment == "Aligned")
All_Data$Moderate_ID[ok] <- All_Data$ID[ok]
ok <- with(All_Data, Type == "Moderate Sender" & Treatment == "Opposed")
All_Data$Moderate_ID[ok] <- All_Data$ID[ok]
All_Data$Extremist_ID <- NA
ok <- with(All_Data, Type == "Extremist Sender" & Treatment == "Aligned")
All_Data$Extremist_ID[ok] <- All_Data$ID[ok]
ok <- with(All_Data, Type == "Extremist Sender" & Treatment == "Opposed")
All_Data$Extremist_ID[ok] <- All_Data$ID[ok]
# it's a little tricky to populate extremist and moderate messages
# because "a_message", etc. applies to moderates in the single
# sender treatment and to extremists otherwise.
All_Data$Moderate_Message <- NA
All_Data$Moderate_Message_Min <- NA
All_Data$Moderate_Message_Max <- NA
ok <- with(All_Data, Type == "Moderate Sender" & Treatment == "Single")
All_Data$Moderate_Message[ok] <- All_Data$a_message[ok]
All_Data$Moderate_Message_Min[ok] <- All_Data$a_msgmin[ok]
All_Data$Moderate_Message_Max[ok] <- All_Data$a_msgmax[ok]
ok <- with(All_Data, Type == "Moderate Sender" & Treatment != "Single")
All_Data$Moderate_Message[ok] <- All_Data$b_message[ok]
All_Data$Moderate_Message_Min[ok] <- All_Data$b_msgmin[ok]
All_Data$Moderate_Message_Max[ok] <- All_Data$b_msgmax[ok]
All_Data$Extremist_Message <- NA
All_Data$Extremist_Message_Min <- NA
All_Data$Extremist_Message_Max <- NA
ok <- with(All_Data, Type == "Extremist Sender")
All_Data$Extremist_Message[ok] <- All_Data$a_message[ok]
All_Data$Extremist_Message_Min[ok] <- All_Data$a_msgmin[ok]
All_Data$Extremist_Message_Max[ok] <- All_Data$a_msgmax[ok]
All_Data <- subset(All_Data, select = c("Treatment", "Session", "Period",
  "Group", "Type", "ID", "Extremist_ID", "Moderate_ID", "Receiver_ID", "Target",
  "Extremist_Message", "Extremist_Message_Min", "Extremist_Message_Max",
  "Moderate_Message", "Moderate_Message_Min", "Moderate_Message_Max", "Action"))


# Make group data ----
Group_Data <- plyr::ddply(All_Data, plyr::.(Session, Period, Group),
  plyr::mutate, Extremist_ID = mean(Extremist_ID, na.rm = TRUE),
  Moderate_ID = mean(Moderate_ID, na.rm = TRUE),
  Receiver_ID = mean(Receiver_ID, na.rm = TRUE),
  Extremist_Message = mean(Extremist_Message, na.rm = TRUE),
  Extremist_Message_Min = mean(Extremist_Message_Min, na.rm = TRUE),
  Extremist_Message_Max = mean(Extremist_Message_Max, na.rm = TRUE),
  Moderate_Message = mean(Moderate_Message, na.rm = TRUE),
  Moderate_Message_Min = mean(Moderate_Message_Min, na.rm = TRUE),
  Moderate_Message_Max = mean(Moderate_Message_Max, na.rm = TRUE),
  Action = mean(Action, na.rm = TRUE))
Group_Data <- subset(Group_Data, subset = Type == "Receiver",
  select = -c(Type, ID))

# Make message data ----
Moderate_Message_Data <- Group_Data
Moderate_Message_Data <- plyr::rename(Moderate_Message_Data,
  c("Moderate_ID" = "ID", "Moderate_Message" = "Message",
    "Moderate_Message_Min" = "Message_Min",
    "Moderate_Message_Max" = "Message_Max"))
Moderate_Message_Data$Bias <- "Moderate"
Moderate_Message_Data$Shift <- 40
Moderate_Message_Data <- subset(Moderate_Message_Data,
  select = c("Treatment", "Session", "Period", "Group", "ID", "Bias", "Shift",
    "Target", "Message", "Message_Min", "Message_Max", "Action"))
Extremist_Message_Data <- subset(Group_Data, Treatment != "Single")
Extremist_Message_Data <- plyr::rename(Extremist_Message_Data,
  c("Extremist_ID" = "ID", "Extremist_Message" = "Message",
    "Extremist_Message_Min" = "Message_Min",
    "Extremist_Message_Max" = "Message_Max"))
Extremist_Message_Data$Bias <- "Extremist"
Extremist_Message_Data$Shift <- 80
ok <- Extremist_Message_Data$Treatment == "Opposed"
Extremist_Message_Data$Shift[ok] <- -80
Extremist_Message_Data <- subset(Extremist_Message_Data,
  select = c("Treatment", "Session", "Period", "Group", "ID", "Bias", "Shift",
    "Target", "Message", "Message_Min", "Message_Max", "Action"))
Message_Data <- rbind(Moderate_Message_Data, Extremist_Message_Data)
Message_Data <- plyr::mutate(Message_Data,
  N_Messages = Message_Max - Message_Min + 1,
  Payoff = 320 - abs(Target + Shift - Action),
  Trust = Action - Message)
Message_Data$Bias <- factor(Message_Data$Bias,
  levels = c("Moderate", "Extremist"))
Message_Data <- plyr::mutate(Message_Data,
  Sender_Type = paste(Bias, "in\n", Treatment, "Condition"),
  Single_Message = 1 * (N_Messages == 1))
Message_Data$Sender_Type <- factor(Message_Data$Sender_Type,
  levels = c("Moderate in\n Single Condition",
    "Moderate in\n Aligned Condition",
    "Moderate in\n Opposed Condition",
    "Extremist in\n Aligned Condition",
    "Extremist in\n Opposed Condition"))
Message_Data <- plyr::mutate(Message_Data, Period01 = (Period - 1) / 24)
Message_Data <- Message_Data[order(Message_Data$ID),]
Message_Data$Message_Midpoint <- with(Message_Data,
  Message_Min + Message_Max) / 2
Message_Data$Max_N_Messages[Message_Data$Message_Midpoint == 0] <- 201
ok <- Message_Data$Message_Midpoint > 0
Message_Data$Max_N_Messages[ok] <-
  1 + 2 * (100 - Message_Data$Message_Midpoint[ok])
ok <- Message_Data$Message_Midpoint < 0
Message_Data$Max_N_Messages[ok] <-
  1 + 2 * (Message_Data$Message_Midpoint[ok] + 100)
Message_Data$Message_Range_Size <-
  Message_Data$Message_Max - Message_Data$Message_Min
Message_Data$Max_Message_Range_Size <- with(Message_Data,
  ifelse(Message_Midpoint == 0, 200,
    ifelse(Message_Midpoint > 0,
      2 * (100 - Message_Midpoint),
      2 * (100 + Message_Midpoint))))
Message_Data$Single <- 1 * (Message_Data$Treatment == "Single")
Message_Data$Aligned <- 1 * (Message_Data$Treatment == "Aligned")
Message_Data$Opposed <- 1 * (Message_Data$Treatment == "Opposed")

Message_Data$Moderate_Single <-
  1 * (Message_Data$Sender_Type == "Moderate in\n Single Condition")
Message_Data$Moderate_Aligned <-
  1 * (Message_Data$Sender_Type == "Moderate in\n Aligned Condition")
Message_Data$Extremist_Aligned <-
  1 * (Message_Data$Sender_Type == "Extremist in\n Aligned Condition")
Message_Data$Moderate_Opposed <-
  1 * (Message_Data$Sender_Type == "Moderate in\n Opposed Condition")
Message_Data$Extremist_Opposed <-
  1 * (Message_Data$Sender_Type == "Extremist in\n Opposed Condition")
rm(ok)

# Make weighted message data ----
Weighted_Message_Data <- do.call(rbind,
  lapply(1:nrow(Message_Data), function(i) {
    n_messages <- with(Message_Data, Message_Max[i] - Message_Min[i]) + 1
    out <- Message_Data[rep(i, n_messages), ]
    out$Message <- with(Message_Data, seq(Message_Min[i], Message_Max[i]))
    out$Weight <- 1 / n_messages
    out$Actual_Message <- Message_Data$Message[i]
    out
  }))
Weighted_Message_Data <- plyr::mutate(Weighted_Message_Data,
  Period01 = (Period - 1) / 24)


# Make action data ----
Action_Data <- Group_Data
Action_Data <- plyr::rename(Action_Data, c("Receiver_ID" = "ID"))
Action_Data <- subset(Action_Data, select = c("Treatment", "Session", "Period",
  "Group", "ID", "Target", "Extremist_Message", "Extremist_Message_Min",
  "Extremist_Message_Max", "Moderate_Message", "Moderate_Message_Min",
  "Moderate_Message_Max", "Action"))
Action_Data <- plyr::mutate(Action_Data,
  Target_Moderate_Message_distance = abs(Target - Moderate_Message),
  Target_Extremist_Message_distance = abs(Target - Extremist_Message),
  Action_Moderate_Message_distance = abs(Action - Moderate_Message),
  Action_Extremist_Message_distance = abs(Action - Extremist_Message),
  Target_Action_distance =abs(Target - Action))
Action_Data$Single <- 1 * (Action_Data$Treatment == "Single")
Action_Data$Aligned <- 1 * (Action_Data$Treatment == "Aligned")
Action_Data$Opposed <- 1 * (Action_Data$Treatment == "Opposed")
Action_Data$Extremist_Message[is.na(Action_Data$Extremist_Message)] <- 0


# Make sender-type-target data ----
Sender_Type_Target_Data <- plyr::ddply(Message_Data,
  plyr::.(Target, Treatment, Bias, Sender_Type), plyr::summarise,
  Mean_Message = mean(Message), Min_Message = min(Message),
  Q25_Message = quantile(Message, .25), Median_Message = median(Message),
  Q75_Message = quantile(Message, .75), Max_Message = max(Message))

# Make sender data ----
Sender_Data1 <- plyr::ddply(Message_Data,
  plyr::.(Session, Treatment, Bias, Sender_Type, ID), plyr::summarize,
  Mean_N_Messages = mean(N_Messages),
  Mean_Single_Message = mean(Single_Message),
  Cor_Target_Message = cor(Target, Message))
Sender_Data2 <- plyr::ddply(Weighted_Message_Data,
  plyr::.(Session, Treatment, Bias, Sender_Type, ID), plyr::summarize,
  Wtd_Cor_Target_Message = weights::wtd.cor(Target, Message, Weight)[1])
Sender_Data <- merge(Sender_Data1, Sender_Data2,
  by = c("Session", "Treatment", "Bias", "Sender_Type", "ID"))
Sender_Data <- plyr::mutate(Sender_Data,
  Log_Mean_N_Messages = log(Mean_N_Messages),
  Z_Cor_Target_Message = arctanh(Cor_Target_Message),
  Z_Wtd_Cor_Target_Message = arctanh(Wtd_Cor_Target_Message))
Sender_Data <- Sender_Data[order(Sender_Data$ID), ]

# Make receiver data ----
Receiver_Data <- plyr::ddply(Action_Data, plyr::.(Session, Treatment, ID),
  plyr::summarise, Cor_Target_Action = cor(Target, Action),
  Cor_Moderate_Message_Action = cor(Moderate_Message, Action),
  Cor_Extremist_Message_Action = cor(Extremist_Message, Action))
Receiver_Data <- plyr::mutate(Receiver_Data,
  Z_Cor_Target_Action = arctanh(Cor_Target_Action),
  Z_Cor_Moderate_Message_Action = arctanh(Cor_Moderate_Message_Action),
  Z_Cor_Extremist_Message_Action = arctanh(Cor_Extremist_Message_Action))

# Make receiver-sender-type data ----
Receiver_Moderate_Data <- Receiver_Data
Receiver_Moderate_Data$Bias <- "Moderate"
Receiver_Moderate_Data <- plyr::rename(Receiver_Moderate_Data,
  c("Cor_Moderate_Message_Action" = "Cor_Message_Action",
    "Z_Cor_Moderate_Message_Action" = "Z_Cor_Message_Action"))
Receiver_Moderate_Data <- subset(Receiver_Moderate_Data,
  select = c("Session", "Treatment", "Bias", "ID", "Cor_Message_Action",
    "Z_Cor_Message_Action"))
Receiver_Extremist_Data <- subset(Receiver_Data, Treatment != "Single")
Receiver_Extremist_Data$Bias <- "Extremist"
Receiver_Extremist_Data <- plyr::rename(Receiver_Extremist_Data,
  c("Cor_Extremist_Message_Action" = "Cor_Message_Action",
    "Z_Cor_Extremist_Message_Action" = "Z_Cor_Message_Action"))
Receiver_Extremist_Data <- subset(Receiver_Extremist_Data,
  select = c("Session", "Treatment", "Bias", "ID", "Cor_Message_Action",
    "Z_Cor_Message_Action"))
Receiver_Sender_Type_Data <- rbind(Receiver_Extremist_Data,
  Receiver_Moderate_Data)
Receiver_Sender_Type_Data <- plyr::mutate(Receiver_Sender_Type_Data,
  Sender_Type = paste(Bias, "in\n", Treatment, "Condition"))
Receiver_Sender_Type_Data$Sender_Type <- factor(
  Receiver_Sender_Type_Data$Sender_Type, levels = c(
    "Moderate in\n Single Condition", "Moderate in\n Aligned Condition",
    "Moderate in\n Opposed Condition", "Extremist in\n Aligned Condition",
    "Extremist in\n Opposed Condition"))


# Make receiver-message data ----
Receiver_Data1 <- subset(Receiver_Data,
  select = c(Session, Treatment, ID, Z_Cor_Moderate_Message_Action))
Receiver_Data1$Bias <- "Moderate"
Receiver_Data1 <- plyr::rename(Receiver_Data1,
  replace = c("Z_Cor_Moderate_Message_Action" = "Z_Cor_Message_Action"))
Receiver_Data2 <- subset(Receiver_Data,
  select = c(Session, Treatment, ID, Z_Cor_Extremist_Message_Action))
Receiver_Data2$Bias <- "Extremist"
Receiver_Data2 <- plyr::rename(Receiver_Data2,
  c("Z_Cor_Extremist_Message_Action" = "Z_Cor_Message_Action"))
Receiver_Message_Data <- rbind(Receiver_Data1, Receiver_Data2)
Receiver_Message_Data <- plyr::mutate(Receiver_Message_Data,
  Sender_Type = paste(Bias, "in", Treatment, "Condition"))

# Convert to data.table and save ----
setDT(All_Data)
setDT(Group_Data)
setDT(Message_Data)
setDT(Action_Data)
setDT(Receiver_Data)
setDT(Receiver_Message_Data)
setDT(Receiver_Sender_Type_Data)
setDT(Sender_Data)
setDT(Sender_Type_Target_Data)
setDT(Subject_Data)
setDT(Weighted_Message_Data)
if (!file.exists("results")) {
  dir.create("results")
}
save(Action_Data, All_Data, Group_Data, Message_Data,
  Receiver_Data, Receiver_Message_Data, Receiver_Sender_Type_Data, Sender_Data,
  Sender_Type_Target_Data, Subject_Data, Weighted_Message_Data,
  file = "results/data.RData")
